﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace myCollection
{
    public partial class PicView : Form
    {
        private string[] files;
        private int index = 0;

        public PicView(string[] files0, int index0)
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            this.files = files0;
            this.index = index0;
            this.showPic();
        }

        private void showPic()
        {
            label2.Text = "File: " + this.files[index];
            label2.Left = Screen.PrimaryScreen.Bounds.Width - label2.Size.Width - 5;
            Image img = Image.FromFile(Properties.Settings.Default.dirfiles + "\\" + this.files[index]);
            int x0 = 0, y0 = 30;
            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;
            if (img.Width >= (Screen.PrimaryScreen.Bounds.Width - 30) || img.Height >= Screen.PrimaryScreen.Bounds.Height)
            {
                nPercentW = ((float)(Screen.PrimaryScreen.Bounds.Width - 30) / (float)img.Width);
                nPercentH = ((float)Screen.PrimaryScreen.Bounds.Height / (float)img.Height);
                if (nPercentH < nPercentW)
                {
                    nPercent = nPercentH;
                }
                else
                {
                    nPercent = nPercentW;
                }
                int destW = (int)(img.Width * nPercent);
                int destH = (int)(img.Height * nPercent);
                Bitmap b = new Bitmap(destW, destH);
                Graphics g = Graphics.FromImage((Image)b);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(img, 0, 0, destW, destH);
                g.Dispose();
                img = (Image)b;
                if (nPercentH < nPercentW)
                {
                    x0 = (Screen.PrimaryScreen.Bounds.Width - img.Width) / 2;
                }
                else
                {
                    y0 = (Screen.PrimaryScreen.Bounds.Height - img.Height) / 2;
                }
            }
            else
            {
                x0 = (Screen.PrimaryScreen.Bounds.Width - img.Width) / 2;
                y0 = (Screen.PrimaryScreen.Bounds.Height - img.Height) / 2;
            }
            pictureBox1.Left = x0;
            pictureBox1.Top = y0;
            pictureBox1.Image = img;
        }

        private void nextPic()
        {
            this.index++;
            if (index >= files.Length)
            {
                this.index = 0;
            }
            this.showPic();
        }

        private void prevPic()
        {
            this.index--;
            if (index < 0)
            {
                this.index = files.Length - 1;
            }
            this.showPic();
        }

        private void PicView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
            else if (e.KeyCode == Keys.Space || e.KeyCode == Keys.Right)
            {
                this.nextPic();
            }
            else if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Left)
            {
                this.prevPic();
            }
        }
    }
}
